package com.hunnyovo.springframework.beans.factory.support;

import com.hunnyovo.springframework.beans.BeansException;
import com.hunnyovo.springframework.beans.factory.BeanFactory;
import com.hunnyovo.springframework.beans.factory.config.BeanDefinition;

/**
 * 抽象bean工厂
 *
 * @author Create By Cb
 * @date 2022/10/26 18:36
 **/
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String beanName) throws BeansException {
        Object bean = this.getSingleton(beanName);
        if (bean != null) {
            return bean;
        }

        BeanDefinition beanDefinition = this.getBeanDefinition(beanName);
        return this.createBean(beanName, beanDefinition);
    }

    @Override
    public Object getBean(String beanName, Object... args) throws BeansException {
        Object bean = this.getSingleton(beanName);
        if (bean != null) {
            return bean;
        }

        BeanDefinition beanDefinition = this.getBeanDefinition(beanName);
        return this.createBean(beanName, beanDefinition, args);
    }

    /**
     * 获取bean基定义
     *
     * @param beanName:
     * @return {@link BeanDefinition}
     * @author Create By Cb
     * @date 2022/11/18 15:54
     */
    abstract BeanDefinition getBeanDefinition(String beanName);

    /**
     * 创建bean携带参数
     *
     * @param beanName:
     * @param beanDefinition:
     * @return {@link Object}
     * @author Create By Cb
     * @date 2022/11/18 16:07
     */
    abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object... args) throws BeansException;

}
